package com.nextgenapp.fbgraph.util
{
	import flash.external.ExternalInterface;
	
	import mx.utils.URLUtil;
	
	/**
	 * util to help do facebook oauth 2.0.  
	 * 
	 * http://developers.facebook.com/docs/authentication/javascript
	 * 
	 * facebook oauth2.0 has 4 different flows: website, javascript, desktop, mobile web.
	 * 
	 * this util is for javascript flow.  
	 * using javascript flow, an access_token will be embedded on url.  this util class will 
	 * retrieve access_token from the url.  
	 * 
	 * @author sol wu - apiswswsw@gmail.com
	 */
	public class OAuthUtil
	{
		public function OAuthUtil()
		{
		}


		/**
		 * retrieve access token from url fragment.
		 */
		public static function retrieveAccessToken():String {
			var access_token:String = null;
			// browsermanager works when we include swf using the generated html file.  if we only use simple <object> tag, browser manager does not work.  
//			var bm:IBrowserManager = BrowserManager.getInstance(); 
//			bm.init("", "Welcome!")
//			var fragments:Object = URLUtil.stringToObject(bm.fragment);
			
			//var myUrl:String = Application.application.url; // this gets the url of swf object instead.
			var href:String = ExternalInterface.call("window.location.href.toString"); // this works.  but require allowScriptAccess="always"
			
			// get url fragment (the part after #)
			var split:Array = href.split("#");
			if (split.length >= 2) {
				var fragment:String = split[1];
				
				var fragments:Object = URLUtil.stringToObject(fragment, "&");
				access_token = fragments.access_token;
			}


			//var obj = Application.application.loaderInfo.loaderURL;
			//var splitURL:Array = Application.application.loaderInfo.loaderURL.split('/');
			//var baseURL:String = "http://"+splitURL[2]; 
			
			//return fragments.access_token;
			return access_token;
		}
	}
}